<?php

namespace app\api\controller;

use app\api\model\Common;
use think\Controller;
use think\Db;
use think\Cookie;
use app\api\model\Preg;
use app\api\model\News;
use app\api\model\Oss;
use app\api\model\Form as order_list;

class Form extends Index
{
	
	 /**
     * 身份证认证
     * @var string
     */
    static $appcode = "b6db53e917c14991a20d02618b786eba";
    static $host = "https://naidcard.market.alicloudapi.com";//身份证认证
    static $url = "https://dm-51.data.aliyun.com/rest/160601/ocr/ocr_idcard.json";//身份证图片识别
    static $hosts = "https://dm-58.data.aliyun.com";//营业执照图片识别
    static $path = "/rest/160601/ocr/ocr_business_license.json";//营业执照图片识别
	
	
    //购物车生成订单
    public function order_start()
    {
        $id = trim(input('get.id'));
        $uid = trim(input('get.uid'));
        $where['id'] = array('in', $id);
        $order_list = new order_list();
        $goods_id = Db::name('shop_cart')->where($where)->column('goods_id');
        foreach ($goods_id as $v) {
            $map['w.id'] = $v;
            $res = Db::name('service_market')->alias('w')->field('w.*,i.username,i.uid')->join('zgh_shop_information i', 'w.s_id=i.id')->where($map)->find();
            $data['order'] = $order_list->rand_order();
            $data['image'] = $res['pic'];
            $data['s_id'] = $res['id'];
            $data['username'] = $uid;
            $data['seller'] = $res['uid'];
            $data['money'] = $res['price'];

            $data['time'] = time();
            $data['statuc'] = 1;
            $data['status'] = 1;
            $data['static'] = 1;
            $data['services'] = 2;
			$data['s_time'] = strtotime("+1 days");
            $info = Db::name('orders')->insert($data);
        }
        $data1['status'] = 2;
        $flag = Db::name('shop_cart')->where($where)->update($data1);
        if ($flag) {
            action_log_index('服务商品生成订单', 'log_ment', 1);
            $this->_jsonp('200', '提交成功', null);
        } else {
            action_log_index('服务商品生成订单', 'log_ment');
            $this->_jsonp('1000', '提交失败', null);
        }
    }

    //直接购买生成订单
    public function direct_start()
    {
        $id = trim(input('get.id'));//商品id
        $uid = trim(input('get.uid'));
        $where['w.id'] = $id;
        $order_list = new order_list();
        $res = Db::name('service_market')->alias('w')->field('w.*,i.username,i.uid')->join('zgh_shop_information i', 'w.s_id=i.id')->where($where)->find();
        $data['order'] = $order_list->rand_order();
        $data['image'] = $res['pic'];
        $data['s_id'] = $res['id'];
        $data['username'] = $uid;
        $data['seller'] = $res['uid'];
        $data['money'] = $res['price'];

        $data['time'] = time();
        $data['statuc'] = 1;
        $data['status'] = 1;
        $data['static'] = 1;
        $data['services'] = 2;
		$data['s_time'] = strtotime("+1 days");
        $info = Db::name('orders')->insertGetId($data);
        if ($info) {
            action_log_index('服务商品生成订单', 'log_ment', 1);
            $this->_jsonp('200', '提交成功', $info);
        } else {
            action_log_index('服务商品生成订单', 'log_ment');
            $this->_jsonp('1000', '提交失败', null);
        }
    }

    //个人中心订单列表
    public function order_index()
    {
        $order = (isset($_GET['order']) && !empty($_GET['order'])) ? $_GET['order'] : '';
        //$services = (isset($_GET['services']) && !empty($_GET['services'])) ? $_GET['services'] : 1;
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $sheet = trim(input('get.sheet'));//$sheet表示符，为1是买家个人订单中心，为2时卖家订单中心
        $uid = trim(input('get.uid'));
        $where['w.services'] = 2;//表服务市场
        $order_list = new order_list('orders');
        if ($sheet == 1) {
            $where['w.username'] = $uid;
            $map['w.username'] = $uid;
            $map['w.statuc'] = array('neq', 0);
            if ($order) {
                $where['w.statuc'] = array('in', $order);
            } else {
                $where['w.statuc'] = array('neq', 0);
            }
            $joins = 'w.seller=u.uid';
            $groups = 'w.statuc,count(w.statuc) as num';
            $gr = 'w.statuc';
        } else if ($sheet == 2) {
            $where['w.seller'] = $uid;
            $map['w.seller'] = $uid;
            $map['w.status'] = array('neq', 0);
            if ($order) {
                $where['w.status'] = array('in', $order);
            } else {
                $where['w.status'] = array('neq', 0);
            }
            $joins = 'w.username=u.uid';
            $groups = 'w.status,count(w.status) as num';
            $gr = 'w.status';
        }
        $type1 = 'zgh_service_market m';
        $condition1 = 'w.s_id=m.id';
        $type2 = 'zgh_service_type t';
        $condition2 = 'm.type=t.id';
        $type3 = 'zgh_shop_information i';
        $condition3 = 'w.seller=i.uid';
        $fields = 'w.*,m.goods_name,m.pic,t.t_name,i.name as shop_name,u.username as user';
        $data = Db::name('orders')->alias('w')->field($fields)
            ->join($type1, $condition1, 'left')
            ->join($type2, $condition2, 'left')
            ->join($type3, $condition3, 'left')->join('zgh_user u', $joins, 'left')
            ->where($where)
            ->order('w.time desc')
            ->limit(10)
            ->page($page)->select();
        $map['w.services'] = 2;

        $num = $order_list->order_group($type1, $type2, $type3, $map, $condition1, $condition2, $condition3, $groups, $gr);
        $news = new News('orders');
        $count = $news->count($where, 'w');
        $Page = new \app\api\model\Page($count, 10);
        $render = $Page->render;
        $res['count'] = $count;
        $res['data'] = $data;
        $res['render'] = $render;
        $res['num'] = $num;
        if (isset($data) && !empty($data)) {
            $this->_jsonp('200', '订单列表数据获取成功', $res);
        } else {
            $this->_jsonp('1000', '订单列表数据获取失败', $res);
        }

    }

    //个人中心订单詳情
    public function order_details()
    {
        $services = trim(input('get.type'));//type值1表示是trading；2是service
        $order_list = new order_list('orders');
        if ($services == 2) {
            $id = trim(input('get.id'));//訂單id
            $where['w.id'] = $id;
            $type1 = 'zgh_service_market m';
            $condition1 = 'w.s_id=m.id';
            $type2 = 'zgh_service_type t';
            $condition2 = 'm.type=t.id';
            $type3 = 'zgh_shop_information i';
            $condition3 = 'w.seller=i.uid';
//            $fields = 'w.*,m.goods_name,m.pic,t.t_name,i.name as shop_name';
           $fields = 'w.services,w.id,w.statuc,w.time,w.order,w.order,w.express_username,w.express_seller,w.image,m.goods_name as username,w.money as store,t.t_name as trading_name,i.name as market_name,i.name,u.alipay';
            $data =Db::name('orders')->alias('w')->field($fields)
                ->join($type1,$condition1,'left')
                ->join($type2,$condition2,'left')
                ->join($type3,$condition3,'left')
                ->join('zgh_user u','u.uid=w.seller','left')
                ->where($where)
                ->find();
            $list_name = $order_list->getForm();
            foreach ($list_name as $k => $v) {
                if ($data['statuc'] == $k) {
                    $data['list_name'] = $v;
                }
            }
			
            $data['count1'] = db('order_msg')->order('time desc')->where('type', 2)->where('order_status', 3)->where('order_num', $data['id'])->value('time');
            $data['count2'] = db('order_msg')->order('time desc')->where('type', 1)->where('order_status', 4)->where('order_num', $data['id'])->value('time');
            $data['count3'] = db('order_msg')->order('time desc')->where('type', 2)->where('order_status', 7)->where('order_num', $data['id'])->value('time');
            $data['count4'] = db('order_msg')->order('time desc')->where('type', 1)->where('order_status', 8)->where('order_num', $data['id'])->value('time');


        } else {
//            $type1 = 'zgh_market_trading m';
//            $condition1 = 'w.name = m.id';
//            $type2 = 'zgh_market_trading_category t';
//            $condition2 = 'm.category_id = t.id';
//            $type3 = 'zgh_market_trading_cid i';
//            $condition3 = 'm.c_id = i.id';
//            $type4 = 'zgh_shop_information s';
//            $condition4 = 'm.name = s.id';
//            $fields = 'w.id,w.money,m.image,m.username as goods_name,t.market_name,i.trading_name,s.name as shop_name';
//            $data = $order_list->order_trading($type1, $type2, $type3, $type4, $where, $condition1, $condition2, $condition3, $condition4, $fields);

            $where['a.id'] = trim(input('get.id'));
            $join[0] = "__MARKET_TRADING__ m";
            $join[1] = "m.id = a.name";
            $jo[0] = "__MARKET_TRADING_CID__ c";
            $jo[1] = "m.c_id = c.id";
            $in[0] = "__MARKET_TRADING_CATEGORY__ t";
            $in[1] = "m.category_id = t.id";
            $ji[0] = "__SHOP_INFORMATION__ s";
            $ji[1] = "m.name = s.id";
            $field = "a.services,a.id,a.statuc,a.order,a.time,a.money as store,m.image,m.username,c.trading_name,t.market_name,s.name,u.alipay";
            $data = db('orders')->alias('a')->field($field)
                ->join($join[0], $join[1])->join($jo[0], $jo[1])->join($in[0], $in[1])->join($ji[0], $ji[1])->join('zgh_user u','u.uid=a.seller','left')->where($where)->find();
			$where1['uid'] = trim(input('get.uid'));
			$where1['order_id'] = $data['id'];
			$data['money'] = db('offer')->where($where1)->value('store');
			$data['king'] = db('orders')->join($join[0],$join[1])->alias('a')->where($where)->value('m.store');
			$data['static'] = db('offer')->where($where1)->value('static');
            $data['count1'] = db('order_msg')->order('time desc')->where('order_status', 2)->where('order_num', $data['id'])->value('time');
            $data['count2'] = db('order_msg')->order('time desc')->where('order_status', 3)->where('order_num', $data['id'])->value('time');
            $data['count3'] = db('order_msg')->order('time desc')->where('order_status', 5)->where('order_num', $data['id'])->value('time');
            $data['count4'] = db('order_msg')->order('time desc')->where('order_status', 6)->where('order_num', $data['id'])->value('time');
            $data['count5'] = db('order_msg')->order('time desc')->where('order_status', 9)->where('order_num', $data['id'])->value('time');


        }
        if (isset($data) && !empty($data)) {
            $this->_jsonp('200', '评论列表数据获取成功', $data);
        } else {
            $this->_jsonp('1000', '评论列表数据获取失败', null);
        }
    }

    //待付款订单，订单上传文件扫描件
    public function msg_upload()
    {
        $id = trim(input('post.id'));//订单id
        $map['id'] = $id;
        $res = Db::name('orders')->where($map)->find();
        if ($res['statuc'] == 10) {
            $this->json('1002', '买方已申请退款', null);
        }
        if ($res['index'] == 3) {
            $this->json('1001', '上传超过次数', null);
        }
        if ($res['statuc'] == 5) {
            $this->json('1001', '买方已确认合同，无法上传', null);
        }
        if (implode('', $_FILES['upfile']['name'])) {
            $show = array();
            //oss上传
//            $ossClient = Oss::getInstance();
//            $bucket = Oss::getBucketName();
//            foreach($_FILES['upfile']['name'] as $key=>$v){
//               if($v){
//                   $ext = substr($v, stripos($v, '.'));
//                   $rand = md5(time() . rand(100, 999));
//                   $object = "contract/" . $rand . $ext;
//                   $content = $_FILES['upfile']['tmp_name'][$key];
//                   $result = $ossClient->uploadFile($bucket, $object, $content);
//                   $show[]=$result['oss-request-url'];
//               }
//            }
            //本地上传
            $files = request()->file('upfile');
            $news_list = new News();
            foreach ($files as $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $news_list->upload($file, 'Images/contract');
                $show[] = $info;
            }
            // echo json_encode($show);die;
            if ($show) {
                // action_log('上传合同文件', 'log_admin',1);
                //上传成功，对orders表修改状态和order_msg表添加信息
                $data['order_status'] = 3;//上传合同
				$data['type'] = 2;//上传合同
                $data['order_num'] = $id;
                $data['order_msg'] = '卖家已上传合同,等待下载';
                $data['time'] = time();
                $where['id'] = $id;
                $data1['statuc'] = 3;
                $data1['status'] = 3;
                $data1['contract'] = json_encode($show);
                $standard = 1;
                //启动事务
                Db::startTrans();
                try {
                    Db::name('order_msg')->insert($data);
                    Db::name('orders')->where($where)->update($data1);
                    Db::name('orders')->where($where)->setInc('index');
                    Db::commit();
                    $standard = 2;
                } catch (\Exception $e) {
                    Db::rollback();
                }
                if ($standard == 2) {
                    action_log_index('服务商品上传合同', 'log_ment', 1);
                    $this->json('200', '提交成功', null);
                } else {
                    action_log_index('服务商品上传合同', 'log_ment');
                    $this->json('1000', '提交失败', null);
                }
            } else {
                //action_log('上传合同文件', 'log_admin');
                $this->json('1000', '上传失败', null);
            }
        } else {
            $this->json('1000', '未上传图片', null);
        }
    }

    //买家订单状态修改
    public function buyer_edit()
    {
        $where['id'] = trim(input('get.id'));//订单id
        $status = trim(input('get.status'));//卖家状态
        $express_username= trim(input('get.username_e'));//买家快递号
        $express_seller = trim(input('get.seller_e'));//卖家快递号
        if(isset($express_username)&&!empty($express_username)){
            $data['username_e']=$express_username;
			$res1= Db::name('orders')->where($where)->find();
			if($res1['status']==6){
				$data['static']=6;
			}
        }
        if(isset($express_seller)&&!empty($express_seller)){
            $data['seller_e']=$express_seller;
        }
        if(isset($status)&&!empty($status)){
            $data['status']=$status;
        }
        $order_status=trim(input('get.statuc'));//买家订单状态
        if($order_status==12){
            $data['status']=14;
            $data['static']=5;
        }
        $order_list = new order_list('orders');
        $list_name = $order_list->getForm();
        $data['statuc'] = $order_status;
        $data1['order_status'] = $order_status;
        $data1['order_num'] = $where['id'];
        $data1['order_msg'] = $list_name[$order_status];//上传订单号
        $data1['time'] = time();
        $standard = 1;
        //启动事务
        Db::startTrans();
        try {
            Db::name('order_msg')->insert($data1);
            Db::name('orders')->where($where)->update($data);
            Db::commit();
            $standard = 2;
        } catch (\Exception $e) {
            Db::rollback();
        }
        if ($standard == 2) {
            action_log_index('服务商品修改订单状态', 'log_ment', 1);
            $this->_jsonp('200', '提交成功', null);
        } else {
            action_log_index('服务商品修改订单状态', 'log_ment');
            $this->_jsonp('1000', '提交失败', null);
        }

    }
    //卖家订单状态修改
    public function seller_edit()
    {
        $where['id'] = trim(input('get.id'));//订单id
        $status = trim(input('get.statuc'));//买家状态c
        $express_username= trim(input('get.username_e'));//买家快递号
        $express_seller = trim(input('get.seller_e'));//卖家快递号
		$res = Db::name('orders')->where($where)->find();
        if ($res['statuc'] == 10) {
            $this->json('1002', '买方已申请退款', null);
        }
        if(isset($express_username)&&!empty($express_username)){
            $data['username_e']=$express_username;
        }
        if(isset($express_seller)&&!empty($express_seller)){
            $data['seller_e']=$express_seller;
			$data['address_seller'] = input('get.address');
			$res1= Db::name('orders')->where($where)->find();
			if($res1['statuc']==5){
				$data['static']=6;
			}
        }
        if(isset($status)&&!empty($status)){
            $data['statuc']=$status;
        }
        $order_status=trim(input('get.status'));//卖家订单状态
        $order_list = new order_list('orders');
        $list_name = $order_list->getForm();
        $data['status'] = $order_status;
        $data1['order_status'] = $order_status;
		$data1['type'] = 2;
        $data1['order_num'] = $where['id'];
        $data1['order_msg'] = $list_name[$order_status];//上传订单号
        $data1['time'] = time();
        $standard = 1;
        //启动事务
        Db::startTrans();
        try {
            Db::name('order_msg')->insert($data1);
            Db::name('orders')->where($where)->update($data);
            Db::commit();
            $standard = 2;
        } catch (\Exception $e) {
            Db::rollback();
        }
        if ($standard == 2) {
            action_log_index('服务商品修改订单状态', 'log_ment', 1);
            $this->_jsonp('200', '提交成功', null);
        } else {
            action_log_index('服务商品修改订单状态', 'log_ment');
            $this->_jsonp('1000', '提交失败', null);
        }

    }

    //买家确认收到合同支付
    public function buyer_receive()
    {
        $where['id'] = input('get.id');//订单id
        $map['uid'] = input('get.uid');
        $statuc = input('get.statuc');//买家订单状态
        $code = input('get.code');//支付密码
        $news = new News('user');
        $common = new Common();
        $res = $news->check_name($map);
        /***
         *$code与支付密码对比，成功则收到
         * //确认合同时
         * **/
        $pass = $common->change_pwd($code, $res['alipay_rand']);
        if ($pass == $res['code']) {
            $data['statuc'] = $statuc;
            
            $data1['order_msg'] = '买家确认接收合同';
            $data1['order_status'] = $statuc;
            $data1['order_num'] = $where['id'];
            $data1['time'] = time();
			$show=Db::name('orders')->where($where)->find();
			if($show['status']==8){
				$data['static'] = 3;
			}
			$goods['id']=$show['s_id'];
			$shops=Db::name('service_market')->where($goods)->find();
			$shop['id']=$shops['s_id'];
            $standard = 1;
            //启动事务
            Db::startTrans();
            try {
                Db::name('order_msg')->insert($data1);
                Db::name('orders')->where($where)->update($data);
				Db::name('service_market')->where($goods)->setInc('volume');
				Db::name('shop_information')->where($shop)->setInc('shop_volume');
                Db::commit();
                $standard = 2;
            } catch (\Exception $e) {
                Db::rollback();
            }
            if ($standard == 2) {
                action_log_index('服务商品收到合同确认支付', 'log_ment', 1);
                $this->_jsonp('200', '确认成功', null);
            } else {
                action_log_index('服务商品收到合同确认支付', 'log_ment');
                $this->_jsonp('1000', '确认失败', null);
            }
        } else {
            $this->_jsonp('1000', '支付密码不正确', null);
        }
    }
    //卖家确认收到合同支付
    public function seller_receive()
    {
        $where['id'] = trim(input('get.id'));//订单id
        $map['uid'] = trim(input('get.uid'));
        $status = trim(input('get.status'));//卖家订单状态
        $code = trim(input('get.code'));//支付密码
        $news = new News('user');
        $common = new Common();
        $res = $news->check_name($map);
        /***
         *$code与支付密码对比，成功则收到
         * //确认合同时
         * **/
        $pass = $common->change_pwd($code, $res['alipay_rand']);
        if ($pass == $res['code']) {
            $data['status']=$status;
			$show=Db::name('orders')->where($where)->find();
			if($show['statuc']==8||$show['statuc']==9){
				$data['static'] = 3;
			}
            $data1['order_msg'] = '卖家确认接收合同';
            $data1['order_status'] = $status;
			$data1['type'] = 2;
            $data1['order_num'] = $where['id'];
            $data1['time'] = time();
            $standard = 1;
            //启动事务
            Db::startTrans();
            try {
                Db::name('order_msg')->insert($data1);
                Db::name('orders')->where($where)->update($data);
                Db::commit();
                $standard = 2;
            } catch (\Exception $e) {
                Db::rollback();
            }
            if ($standard == 2) {
                action_log_index('服务商品收到合同确认支付', 'log_ment', 1);
                $this->_jsonp('200', '确认成功', null);
            } else {
                action_log_index('服务商品收到合同确认支付', 'log_ment');
                $this->_jsonp('1000', '确认失败', null);
            }
        } else {
            $this->_jsonp('1000', '密码不正确', null);
        }
    }

    //申诉
    public function appeal()
    {
        $data['uid'] = trim(input('post.uid'));
//        $data['username'] = trim(input('get.username'));
        $data['order'] = trim(input('post.order'));//订单号
        $data['image'] = trim(input('post.image'));//商品图片
        $data['problem'] = htmlspecialchars(trim(input('post.problem')));
        $data['nick'] = htmlspecialchars(trim(input('post.nick')));
        $data['status'] = trim(input('post.status'));
        $data['phone'] = htmlspecialchars(trim(input('post.phone')));
        $data['time'] = time();
		$data['type'] = trim(input('post.type'));
//        $data['category_id'] = trim(input('post.category_id'));
        $news_list = new News('complain');
        if (!empty($_FILES)) {
            $files = request()->file('upfile');
            $img = array();
            foreach ($files as $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
				//
            $match = new Preg();
            $token=Cookie::get('access_token','think_');
            if(empty($token)){
                $info= $match->get_tooken();
                $arrs=json_decode($info, true);
                $token=$arrs["access_token"];
                Cookie::set('access_token',$token,1728000);//使用think\Cookie
            }
            $res=$match->get_img($token,$_FILES['upfile']['tmp_name']);
            $results=json_decode($res, true);
            if($results['conclusion']!='合规'){
                $this->json('1000', '图片不合规范', null);
            }
            //
                $info = $news_list->upload($file, 'Images/appeal');
                $img[] = $info;
            }
            $data['pic'] = json_encode($img);
        }
        $flag = Db::name('complain')->insert($data);
        if ($flag) {
            action_log_index('交易与服务商品申诉', 'log_ment', 1);
            $this->json('200', '申诉成功', null);
        } else {
            action_log_index('交易与服务商品申诉', 'log_ment');
            $this->json('1000', '申诉失败', null);
        }
    }

    //判断订单是否逾期
    public function judge_order()
    {
        $where['statuc'] = 1;
		$where['status'] = 1;
		 $where['services'] = 2;
        $time = time() - 24 * 60 * 60;
        $where['time'] = ['<', $time];
        $data = Db::name('orders')->where($where)->column('id');
        $ids = implode(',', $data);
        $show['static'] = 5;
		$show['statuc'] = 12;
		$show['status'] = 14;
        Db::name('orders')->where('id', 'in', $ids)->update($show);
    }

    //申请退款
    public function application()
    {
        $where['id'] = trim(input('post.id'));
        $data['statuc'] = 10;
        $data['status'] = 11;
        $data1['problem'] = htmlspecialchars(trim(input('get.reason')));
        $data1['uid'] = trim(input('get.uid'));
        $data1['username'] = trim(input('get.username'));
        $data1['order'] = trim(input('get.order'));//订单号
        $data1['image'] = trim(input('get.pic'));//商品图片
        $data1['nick'] = htmlspecialchars(trim(input('get.nick')));
        $data1['phone'] = htmlspecialchars(trim(input('get.phone')));
        $data1['time'] = time();
         $res= Db::name('orders')->where($where)->find();
        $new['statuc']=$res['statuc'];
        $new['status']=$res['status'];
        $new['msg']='卖家申请退款';
        $data2['order_num'] = input('post.id');;
        $data2['order_status'] = 10;
        $data2['order_msg'] = json_encode($new);
        $data2['time'] = time();

        $standard = 1;
        //启动事务
        Db::startTrans();
        try {
            Db::name('orders')->where($where)->update($data);
			Db::name('order_msg')->insert($data2);
            Db::name('complain')->insert($data1);
            Db::commit();
            $standard = 2;
        } catch (\Exception $e) {
            Db::rollback();
        }
        if ($standard == 2) {
            action_log_index('服务商品申请退款', 'log_ment', 1);
            $this->json('200', '提交成功', null);
        } else {
            action_log_index('服务商品申请退款', 'log_ment');
            $this->json('1000', '提交失败', null);
        }
    }

    //买家评价
    public function order_comment()
    {
        $where['id'] = input('post.id');
        $res = Db::name('orders')->where($where)->find();
        $data['sheet'] = 1;
        $data['g_id'] = $res['s_id'];//商品id
        $data['order_number'] = $res['order'];//订单号
        $data['buyer_id'] = $res['username'];
        $data['seller_id'] = $res['seller'];//买家id
        $data['seller_name'] = input('post.shop_name');//店铺名称
        $data['a_id'] = 0;
        $data['type'] = input('post.first');
        $data['state'] = input('post.type');
        $data['content'] = htmlspecialchars(trim(input('post.news_title')));
		$goods['id']=$res['s_id'];
        $is_an = input('post.is_an');
        if ($is_an) {
            $data['is_an'] = 1;
        } else {
            $data['is_an'] = 2;
        }
        $data['time'] = time();
        $news_list = new News('order_comment');
        if (!empty($_FILES)) {
            $files = request()->file('upfile');
            $img = array();
            foreach ($files as $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
				 // 
            $match = new Preg();
            $token=Cookie::get('access_token','think_');
            if(empty($token)){
                $info= $match->get_tooken();
                $arrs=json_decode($info, true);
                $token=$arrs["access_token"];
                Cookie::set('access_token',$token,1728000);//使用think\Cookie
            }
            $res=$match->get_img($token,$_FILES['upfile']['tmp_name']);
            $results=json_decode($res, true);
            if($results['conclusion']!='合规'){
                $this->json('1000', '图片不合规范', null);
            }
            //
                $info = $news_list->upload($file, 'Images/comment');
                $img[] = $info;
            }
            $data['pic'] = json_encode($img);
        }
        $data1['statuc'] = 9;
		$data2['order_num'] = input('post.id');;
        $data2['order_status'] = 9;
		$data2['type'] = 1;
        $data2['order_msg'] = '买家完成评价';
        $data2['time'] = time();
        $standard = 1;
        //启动事务
        Db::startTrans();
        try {
            Db::name('orders')->where($where)->update($data1);
            Db::name('order_comment')->insert($data);
			Db::name('service_market')->where($goods)->setInc('rate');
            Db::commit();
            $standard = 2;
        } catch (\Exception $e) {
            Db::rollback();
        }
        if ($standard == 2) {
            action_log_index('服务商品买家评价', 'log_ment', 1);
            $this->json('200', '提交成功', null);
        } else {
            action_log_index('服务商品买家评价', 'log_ment');
            $this->json('1000', '提交失败', null);
        }
    }

    //卖家评价
    public function order_seller()
    {
        $where['id'] = trim(input('post.id'));
        $res=Db::name('orders')->where($where)->find();
		$flag=db('order_comment')->where('order_number', $res['order'])->where('a_id', 0)->where('sheet', 2)->find();
		if($flag){
			$this->json('1000', '已经提交,勿重复提交', null);
		}
        $data['sheet'] = 2;
        $data['g_id'] = $res['s_id'];//商品id
        $data['order_number'] = $res['order'];//订单号
        $data['buyer_id'] = $res['username'];
        $data['seller_id'] = $res['seller'];//买家id
        $data['seller_name'] = trim(input('post.shop_name'));//店铺名称
        $data['state'] = trim(input('post.type'));
        $data['content'] = htmlspecialchars(trim(input('post.news_title')));
        $is_an = trim(input('post.is_an'));
        if ($is_an) {
            $data['is_an'] = 1;
        } else {
            $data['is_an'] = 2;
        }
        $data['time'] = time();

        $data1['status'] = 11;
        $where1['order'] = $data['order_number'];
        $where2['id'] = $data['g_id'];
        $data2['order_num'] = trim(input('post.id'));
        $data2['order_status'] = 11;
		$data2['type'] = 2;
        $data2['order_msg'] = '卖家已完成评价';
        $data2['time'] = time();
        $standard = 1;
        //启动事务
        Db::startTrans();
        try {
            Db::name('orders')->where($where1)->update($data1);
            Db::name('order_msg')->insert($data2);
            Db::name('order_comment')->insert($data);
            Db::commit();
            $standard = 2;
        } catch (\Exception $e) {
            Db::rollback();
        }
        if ($standard == 2) {
            action_log_index('服务商品卖家评价', 'log_ment', 1);
            $this->json('200', '提交成功', null);
        } else {
            action_log_index('服务商品卖家评价', 'log_ment');
            $this->json('1000', '提交失败', null);
        }
    }

    /*个人中心评价列表**/
    public function order_comment_list()
    {
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $sheet = (isset($_GET['sheet']) && !empty($_GET['sheet'])) ? $_GET['sheet'] : 1;
        $where['b.sheet'] = $sheet;
        $index = (isset($_GET['index']) && !empty($_GET['index'])) ? $_GET['index'] : array('neq', 0);
        $where['b.state'] = $index;
        $news_list = new News('order_comment');
        $type1 = 'zgh_user u';
        $condition1 = 'b.buyer_id=u.uid';
        $fields = 'b.*,u.head_img';
        $data = $news_list->two_list($where, $type1, $condition1, 10, $page, $fields);
        $count = $news_list->count($where, 'b');
        //分页
        $Page = new \app\api\model\Page($count, 10);
        $render = $Page->render;
        //$res = model('Common')->page_data($count, $page, 10);
        $res['count'] = $count;
        $res['data'] = $data;
        $res['render'] = $render;
        if (isset($data) && !empty($data)) {
            $this->_jsonp('200', '评论列表数据获取成功', $res);
        } else {
            $this->_jsonp('1000', '评论列表数据获取失败', null);
        }
    }

    //添加地址
    public function add_address()
    {
        $data['username'] = trim(input('get.uid'));
        $data['nick'] = trim(input('get.nick'));
        $data['phone'] = trim(input('get.phone'));
        $data['address'] = trim(input('get.address'));
        $data['code'] = trim(input('get.code'));
        $data['location'] = trim(input('get.location'));
        $is_default = trim(input('get.is_default'));
        $data['is_default'] = $is_default;
        $data['static'] = trim(input('get.static'));
        $data['time'] = time();
        $form_list = new order_list('orders_address');
        $where['static'] = 1;
        $where['username'] = trim(input('get.uid'));
        $where['is_default'] = array('in', array(1, 2));
        $address = db('orders_address')->where($where)->count();
        if ($address >= 6) {
            $this->_jsonp('1000', '已保存6条数据,无法添加', null);
        } else {
            //若为默认
            if ($is_default == 1) {
                $show['is_default'] = 2;
                $map['username'] = trim(input('get.uid'));
                $map['is_default'] = array('neq', 3);
                $form_list->edit_form($map, $show);
            }
            $res = $form_list->add_form($data);
            if (isset($res) && $res) {
                action_log_index('新增地址', 'log_ment', 1);
                $this->_jsonp('200', '添加地址成功', $res);
            } else {
                action_log_index('新增地址', 'log_ment');
                $this->_jsonp('1000', '添加地址失败', null);
            }
        }
    }

    //修改地址
    public function edit_address()
    {
        $where['id'] = trim(input('get.id'));
        $data['username'] = trim(input('get.uid'));
        $data['nick'] = trim(input('get.nick'));
        $data['phone'] = trim(input('get.phone'));
        $data['address'] = trim(input('get.address'));
        $data['code'] = trim(input('get.code'));
        $data['location'] = trim(input('get.location'));
        $is_default = trim(input('get.is_default'));
        $data['is_default'] = $is_default;
        $data['static'] = trim(input('get.static'));
        $form_list = new order_list('orders_address');
        //若为默认
        if ($is_default == 1) {
            $show['is_default'] = 2;
            $map['username'] = trim(input('get.uid'));
            $map['is_default'] = array('neq', 3);
            $form_list->edit_form($map, $show);
        }
        $res = $form_list->edit_form($where, $data);
        if (isset($res) && $res) {
            action_log_index('修改地址', 'log_ment', 1);
            $this->_jsonp('200', '添加地址成功', $res);
        } else {
            action_log_index('修改地址', 'log_ment');
            $this->_jsonp('1000', '添加地址失败', null);
        }
    }

    //删除地址
    public function address_del()
    {
        $where['id'] = trim(input('get.id'));
        $data['is_default'] = trim(input('get.is_default'));
        $form_list = new order_list('orders_address');
        //若为默认
        if ($data['is_default'] == 1) {
            $show['is_default'] = 2;
            $map['username'] = trim(input('get.uid'));
            $map['is_default'] = array('neq', 3);
            $form_list->edit_form($map, $show);
        }
        $res = $form_list->edit_form($where, $data);
        if ($res == true) {
            action_log_index('删除地址', 'log_ment', 1);
            $this->_jsonp('200', '删除成功', $res);
        } else {
            action_log_index('删除地址', 'log_ment');
            $this->_jsonp('1000', '删除失败', null);
        }
    }

    //前台获取地址列表
    public function address_list()
    {
        $where['username'] = trim(input('get.uid'));
        $where['is_default'] = array('neq', 3);
        $form_list = new order_list('orders_address');
        $order = 'is_default asc,time desc';
        $data = $form_list->form_list($where, "*", $order, 6);
        if (isset($data) || $data) {
            $this->_jsonp('200', '列表查询成功', $data);
        } else {
            $this->_jsonp('1000', '没有数据', null);
        }
    }

    //前台获取地址详情
    public function address_detail()
    {
        $where['id'] = trim(input('get.id'));
        $data = Db::name('orders_address')->where($where)->find();
        if (isset($data) && $data) {
            $this->_jsonp('200', '查询成功', $data);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }

    //评价管理
    public function comment_list()
    {
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $sheet = (isset($_GET['sheet']) && !empty($_GET['sheet'])) ? $_GET['sheet'] : 2;//1代表买家，2代表卖家
        $state = (isset($_GET['state']) && !empty($_GET['state'])) ? $_GET['state'] : array('neq', 0);
        $index = trim(input('get.index'));//1代表个人中心，2代表卖家
        $uid = trim(input('get.uid'));
        if ($sheet == 1) {
            $where['state'] = $state;
        } else {
            $where['state'] = 0;
        }
        if ($index == 1) {
            $where['buyer_id'] = $uid;
        } else {
            $where['seller_id'] = $uid;
        }
        $news_list = new News('order_comment');
        $data = Db::name('order_comment')->where($where)->limit(10)->page($page)->order('time desc')->select();
        $count = $news_list->count($where, 'b');
        $Page = new \app\api\model\Page($count, 10);
        $render = $Page->render;
        $res['count'] = $count;
        $res['data'] = $data;
        $res['render'] = $render;
        if (isset($data) && !empty($data)) {
            $this->_jsonp('200', '评论列表数据获取成功', $res);
        } else {
            $this->_jsonp('1000', '评论列表数据获取失败', null);
        }
    }

    //账户安全个人资料
    public function user_detail()
    {
        $where['uid'] = trim(input('get.uid'));
        $news_list = new News('user');
        $data = $news_list->check_name($where);
        $res1 = Db::name('authentication_personal')->where($where)->find();
        $res2 = Db::name('authentication_enterprise')->where($where)->find();
        if (isset($res1['static'])) {
            if ($res1['static'] == 2) {
                $data['authentication'] = 3;
            } else {
                $data['authentication'] = 1;
            }
        } else if (isset($res2['static'])) {
            if ($res2['static'] == 2) {
                $data['authentication'] = 4;
            } else {
                $data['authentication'] = 2;
            }
        } else {
            $data['authentication'] = 0;
        }
        if (isset($data) && !empty($data)) {
            $this->_jsonp('200', '数据获取成功', $data);
        } else {
            $this->_jsonp('1000', '数据获取失败', null);
        }
    }
	/*验证是否重复*/
    public function talentname()
    {
        $username = htmlspecialchars(trim(input('get.username')));
        $flag=Db::name('user')->where('username|phone|email',$username)->find();    
        if (isset($flag) && !empty($flag)) {          
            $this->_jsonp('200', '名称重复', null);
        } else {
            $this->_jsonp('1000', '名称可以使用', null);
        }
    }
	//写入不文明词
    public function write(){
        $text=input('get.words');
        $f=fopen('preg.txt', "a+");
        fwrite($f, $text);
        fclose($f);
    }
	//生成表格
	public function get_excel(){
        $a=input('get.type');
        $match = new Preg();
        if($a=='service'){
            $fields='w.*,i.name as shop_name,t.t_name';
            $data= Db::name('service_market')->alias('w')->field($fields)
                ->join('zgh_service_type t', 'w.classify=t.id','left')
                ->join('zgh_shop_information i','w.s_id=i.id','left')
                ->where('w.state','<>','3')->select();
            $match->service_excel($data);
        }elseif ($a=='trading'){
            $fields='w.*,s.name as shop_name,m.market_name as t_name,c.trading_name';
            $data= Db::name('market_trading')->alias('w')->field($fields)
                ->join('__MARKET_TRADING_CATEGORY__ m', 'm.id = w.category_id','left')
                ->join('__SHOP_INFORMATION__ s','s.id = w.name','left')
                ->join('__MARKET_TRADING_CID__ c','c.id = w.c_id','left')
                ->where('w.is_show','<>','2')->select();
            $match->trading_excel($data);
        }elseif ($a=='shop'){
            $fields='w.*';
            $data= Db::name('shop_information')->alias('w')->field($fields)->where('w.uid','<>',0)
                ->select();
            $match->shop_excel($data);
        }elseif ($a=='news'){
            $data = Db::name('government_work')->alias('w')->field('w.*,u.username,t.name')
                ->join("zgh_government_type t", 'w.type=t.id')
                ->join('__USER__ u', 'u.uid=w.uid')
                ->where('w.status','<>','3')->select();
            $match->news_excel($data);
        }elseif ($a=='video'){
            $data = Db::name('video')->alias('w')->field('w.*,u.username,t.title,t.pic,c.name')
                ->join("zgh_video_list t", 'w.l_id=t.id')
                ->join("zgh_video_type c", 't.type=c.tid')
                ->join('__USER__ u', 'u.uid=t.uid')
                ->where('w.status','<>','3')->select();
            $match->video_excel($data);
        }


    }

    //账户安全个人资料修改
    public function user_edit()
    {
        $where['uid'] = trim(input('post.uid'));
        if (!empty($_FILES['file']['name'])) {
			//百度验证图片是否合格
			$match = new Preg();
            $token=Cookie::get('access_token','think_');
            if(empty($token)){
                $info= $match->get_tooken();
                $arrs=json_decode($info, true);
                $token=$arrs["access_token"];
                Cookie::set('access_token',$token,1728000);//使用think\Cookie
            }
            $res=$match->get_img($token,$_FILES['file']['tmp_name']);
            $results=json_decode($res, true);
            if($results['conclusion']!='合规'){
                $this->json('1000', '图片不合规范', null);
            }
			//结束
            $files = request()->file('file');
            $news_list = new News();
            $img = $news_list->upload($files, 'Images/head');
            if (!$img) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['head_img'] = $img;
        }
        $salary = trim(input('post.salary'));
        if (isset($salary) && !empty($salary)) {
            $data['salary'] = $salary;
        }
        $username = htmlspecialchars(trim(input('post.username')));
        if (isset($username) && !empty($username)) {
            $data['username'] = $username;
        }
        $flag=Db::name('user')->where('username|phone|email',$username)->find();    
        if (isset($flag) && !empty($flag)) {          
            $this->json('1000', '名称重复', null);
        }
        $education = trim(input('post.education'));
        if (isset($education) && !empty($education)) {
            $data['education'] = $education;
        }
        $date = trim(input('post.date'));
        if (isset($date) && !empty($date)) {
            $data['date'] = $date;
        }
        $data['sex'] = trim(input('post.sex'));
        $form_list = new order_list('user');
        $res = $form_list->edit_form($where, $data);
        if (isset($res) && !empty($res)) {
            action_log_index('个人中心资料修改', 'log_ment', 1);
            $this->json('200', '数据获取成功', $res);
        } else {
            action_log_index('个人中心资料修改', 'log_ment');
            $this->json('1000', '数据获取失败', null);
        }
    }

    //账户安全认证审核状态信息
    public function examine_personal()
    {
        $where['uid'] = trim(input('get.uid'));
        $status = trim(input('get.status'));
        if ($status == 1) {
            $news_list = new News('authentication_enterprise');
        } else {
            $news_list = new News('authentication_personal');
        }
        $data = $news_list->check_name($where);
        if (isset($data) && !empty($data)) {
            $this->_jsonp('200', '数据获取成功', $data);
        } else {
            $this->_jsonp('1000', '未查询到数据', null);
        }
    }

	
	
	    /**
     * 身份证认证
     */
    public function examine_img()
    {

        $url = self::$url;
        $appcode = self::$appcode;
        //如果输入带有inputs, 设置为True，否则设为False
        $is_old_format = false;
        //如果没有configure字段，config设为空
        $config = array(
            "side" => trim(input('post.side'))//face正面 back//背面
        );
        $news_list = new News();
        $legal = trim(input('post.legal'));
        if ($legal == '2') {
            if ($config['side'] == 'back') {
                if (!empty($_FILES['legal_back']['name'])) {
                    $files = request()->file('legal_back');
                    $file = $news_list->upload($files, 'Images/authentication');
                    if (!$file) {
                        $this->json('1000', '图片上传失败', null);
                    }
                }
            } else {
                if (!empty($_FILES['legal_face']['name'])) {
                    $files = request()->file('legal_face');
                    $file = $news_list->upload($files, 'Images/authentication');
                    if (!$file) {
                        $this->json('1000', '图片上传失败', null);
                    }
                }
            }
        } else if ($legal == '3') {
            if ($config['side'] == 'back') {
                if (!empty($_FILES['charge_back']['name'])) {
                    $files = request()->file('charge_back');
                    $file = $news_list->upload($files, 'Images/authentication');
                    if (!$file) {
                        $this->json('1000', '图片上传失败', null);
                    }
                }
            } else {
                if (!empty($_FILES['charge_face']['name'])) {
                    $files = request()->file('charge_face');
                    $file = $news_list->upload($files, 'Images/authentication');
                    if (!$file) {
                        $this->json('1000', '图片上传失败', null);
                    }
                }
            }
        } else {
            if ($config['side'] == 'back') {
                if (!empty($_FILES['back']['name'])) {
                    $files = request()->file('back');
                    $file = $news_list->upload($files, 'Images/authentication');
                    if (!$file) {
                        $this->json('1000', '图片上传失败', null);
                    }
                }
            } else {
                if (!empty($_FILES['face']['name'])) {
                    $files = request()->file('face');
                    $file = $news_list->upload($files, 'Images/authentication');
                    if (!$file) {
                        $this->json('1000', '图片上传失败', null);
                    }
                }
            }
        }
        $file = ROOT_PATH . "public/Images/authentication/$file";
        if ($fp = fopen($file, "rb", 0)) {
            $binary = fread($fp, filesize($file)); // 文件读取
            fclose($fp);
            $base64 = base64_encode($binary); // 转码
        }
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        //根据API的要求，定义相对应的Content-Type
        array_push($headers, "Content-Type" . ":" . "application/json; charset=UTF-8");
        if ($is_old_format == TRUE) {
            $request = array();
            $request["image"] = array(
                "dataType" => 50,
                "dataValue" => "$base64"
            );

            if (count($config) > 0) {
                $request["configure"] = array(
                    "dataType" => 50,
                    "dataValue" => json_encode($config)
                );
            }
            $body = json_encode(array("inputs" => array($request)));
        } else {
            $request = array(
                "image" => "$base64"
            );
            if (count($config) > 0) {
                $request["configure"] = json_encode($config);
            }
            $body = json_encode($request);
        }
        $method = "POST";
        $name = $this->cms_curl($method, $url, $headers, $body, $is_old_format);
        echo $name;
    }

    /**
     * curl发送请求
     * @param $method
     * @param $url
     * @param $headers
     * @param $body
     * @param $is_old_format
     */
    public function cms_curl($method, $url, $headers, $body, $is_old_format)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$" . $url, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
        $result = curl_exec($curl);
        $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
        $rheader = substr($result, 0, $header_size);
        $rbody = substr($result, $header_size);

        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);
        if ($httpCode == 200) {
            if ($is_old_format) {
                $output = json_decode($rbody, true);
                $result_str = $output["outputs"][0]["outputValue"]["dataValue"];
            } else {
                $result_str = json_decode($rbody, true);
            }
            $this->json('200', '成功！', $result_str);
        } else {
            $this->json('1000', '不是有效证件！', null);
        }
    }

    /**
     * 身份证获取认证信息
     */
    public function examine_image($id, $name)
    {
        $host = self::$host;
        $appcode = self::$appcode;
        $path = "/nidCard";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "idCard=$id&name=$name";
        $url = $host . $path . "?" . $querys;
        $username = $this->curl($url, $headers, $host, 'GET');
        echo $username;
    }

    public function curl($url, $headers, $host, $method)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$" . $host, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $out_put = curl_exec($curl);
        curl_close($curl);
        $out_put = json_decode($out_put, true);
        if ($out_put['status'] != '01') {
            $this->json('1000', $out_put['msg'], null);
        }
    }
 /**
     * 营业执照
     */
    public function examine_file($file)
    {
        $host = self::$hosts;
        $path = self::$path;
        $appcode = self::$appcode;
        $method = "POST";
        $file = ROOT_PATH . "public/Images/company/$file";
        if ($fp = fopen($file, "rb", 0)) {
            $binary = fread($fp, filesize($file)); // 文件读取
            fclose($fp);
            $base64 = base64_encode($binary); // 转码
        }
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        //根据API的要求，定义相对应的Content-Type
        $body = "{\"image\":\"{$base64}\"}";
        $url = $host . $path;
        $name = $this->cmss_curl($method, $url, $headers, $host, $body);
        return $name;
    }

    public function cmss_curl($method, $url, $headers, $host, $bodys)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$" . $host, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }

        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
        $result = curl_exec($curl);
        curl_close($curl);
        $out_put = json_decode($result, true);
        return $out_put;
    }
	
	
    //个人认证添加
    public function examine_add()
    {
        $where['uid'] = trim(input('post.uid'));
        $flag = Db::name('authentication_personal')->where($where)->find();
        if ($flag) {
            $this->json('1000', '请勿重复添加', null);
        }
        $data['nick'] = trim(input('post.nick'));
        $data['card'] = trim(input('post.idcard'));
        $data['uid'] = trim(input('post.uid'));
        $data['phone'] = trim(input('post.phone'));
        $data['alipay'] = trim(input('post.alipay'));
        $data['validity_time'] = trim(input('post.date'));
        $data['time'] = time();
        $data['static'] = 1;
		
		$this->examine_image($data['card'], $data['nick']);
		
		$mytime = date("Y-m-d", strtotime("1 year"));
        if ($data['validity_time'] < $mytime) {
            $this->json('1000', '证件有效期必须大于入驻时间1年', null);
        }
        $news_list = new News();
        if (!empty($_FILES['face']['name'])) {
            $files = request()->file('face');
            $img = $news_list->upload($files, 'Images/authentication');
            if (!$img) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['positive_image'] = $img;
        }
        if (!empty($_FILES['back']['name'])) {
            $files = request()->file('back');
            $img = $news_list->upload($files, 'Images/authentication');
            if (!$img) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['reverse_image'] = $img;
        }
        $form_list = new order_list('authentication_personal');
        $res = $form_list->add_form($data);
        if (isset($res) && !empty($res)) {
            action_log_index('个人中心认证添加', 'log_ment', 1);
            $this->json('200', '数据添加成功', $res);
        } else {
            action_log_index('个人中心认证添加', 'log_ment', 1);
            $this->json('1000', '数据添加失败', null);
        }
    }

    //个人认证修改
    public function examine_edit()
    {
        $data['nick'] = trim(input('post.nick'));
        $data['card'] = trim(input('post.idcard'));
        $where['uid'] = trim(input('post.uid'));
        $data['phone'] = trim(input('post.phone'));
        $data['alipay'] = trim(input('post.alipay'));
        $data['validity_time'] = trim(input('post.date'));
        $data['time'] = time();
        $data['result'] = '';
        $data['static'] = 1;
		
		$this->examine_image($data['card'], $data['nick']);
		
		$mytime = date("Y-m-d", strtotime("1 year"));
        if ($data['validity_time'] < $mytime) {
            $this->json('1000', '证件有效期必须大于入驻时间1年', null);
        }
        $news_list = new News();
        if (!empty($_FILES['face']['name'])) {
            $files = request()->file('face');
            $img = $news_list->upload($files, 'Images/authentication');
            if (!$img) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['positive_image'] = $img;
        }
        if (!empty($_FILES['back']['name'])) {
            $files = request()->file('back');
            $img = $news_list->upload($files, 'Images/authentication');
            if (!$img) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['reverse_image'] = $img;
        }
        $form_list = new order_list('authentication_personal');
        $res = $form_list->edit_form($where, $data);
        if (isset($res) && !empty($res)) {
            action_log_index('个人中心认证修改', 'log_ment', 1);
            $this->json('200', '数据修改成功', $res);
        } else {
            action_log_index('个人中心认证修改', 'log_ment');
            $this->json('1000', '数据修改失败', null);
        }
    }

    //企业认证添加
    public function company_add()
    {
        $where['uid'] = trim(input('post.uid'));
        $flag = Db::name('authentication_enterprise')->where($where)->find();
        if ($flag) {
            $this->json('1000', '请勿重复添加', null);
        }
        $data['uid'] = trim(input('post.uid'));
        $media1 = trim(input('post.media1JsonString'));
        $media2 = trim(input('post.media2JsonString'));
        $media3 = trim(input('post.media3JsonString'));
        $form1 = json_decode($media1, true);
        $form2 = json_decode($media2, true);
        $form3 = json_decode($media3, true);
        $news_list = new News();
        if (!empty($_FILES['business_img']['name'])) {
            $files = request()->file('business_img');
            $img1 = $news_list->upload($files, 'Images/company');
            if (!$img1) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['business_pic'] = $img1;
        }
        if (!empty($_FILES['code_img']['name'])) {
            $files = request()->file('code_img');
            $img2 = $news_list->upload($files, 'Images/company');
            if (!$img2) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['code_pic'] = $img2;
        }
        if (!empty($_FILES['tax_img']['name'])) {
            $files = request()->file('tax_img');
            $img3 = $news_list->upload($files, 'Images/company');
            if (!$img3) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['tax_pic'] = $img3;
        }
        if ($form1['organization'] == 'normal') {
            $data['c_type'] = 1;
            if ($form1['paper'] == 1) {
                $data['d_type'] = 1;
            } else {
                $data['d_type'] = 2;
                //本地上传
                if (!empty($_FILES['single']['name'])) {
                    $files = request()->file('single');
                    $img4 = $news_list->upload($files, 'Images/company');
                    if (!$img4) {
                        $this->json('1000', '图片上传失败', null);
                    }
                    $data['business_pic'] = $img4;
                }
            }
            $data['company_name'] = $form1['enterprises'];
            $data['regist_number'] = $form1['enterpriseNumber'];
        } else {
            $data['c_type'] = 2;
            $data['d_type'] = 1;
            $data['company_name'] = $form1['institutions'];
            $data['regist_number'] = $form1['cards'];
        }
		$data['uid'] = trim(input('post.uid'));
        $data['company_year'] = $form1['date'];
        $data['o_code'] = $form1['organization_code'];
        $data['tax_number'] = $form1['tax'];
		if (strlen($data['tax_number']) == 15 || strlen($data['tax_number']) == 17 || strlen($data['tax_number']) == 18 || strlen($data['tax_number']) == 20) {
        } else {
            $this->json('1000', '税务登记号格式不正确！', null);
            return false;
        }
        $re1 = "/^([0-9])|((?=.*?[0-9])(?=.*?[a-zA-Z])[a-zA-Z0-9])$/";
        if (!preg_match($re1, $data['tax_number'])) {
            $this->json('1000', '税务登记号格式不正确！', null);
            return false;
        }
        //法人信息
        $data['legal_card'] = trim(input('post.legal_card'));
        $data['legal_date'] = trim(input('post.legal_date'));
        $data['legal_name'] = $form2['legal_name'];
		$mytime = date("Y-m-d", strtotime("1 year"));
        if ($data['legal_date'] < $mytime) {
            $this->json('1000', '证件有效期必须大于入驻时间1年！', null);
        }
        if (!empty($_FILES['legal_face']['name'])) {
            $files = request()->file('legal_face');
            $img5 = $news_list->upload($files, 'Images/authentication');
            if (!$img5) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['legal_face'] = $img5;
        }
        if (!empty($_FILES['legal_back']['name'])) {
            $files = request()->file('legal_back');
            $img6 = $news_list->upload($files, 'Images/authentication');
            if (!$img6) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['legal_back'] = $img6;
        }
		$name = $this->examine_file($data['business_pic']);
        if (!$name) {
            $this->json('1000', '请检查您的营业执照！', null);
        }
        if ($name['person'] != $data['legal_name'] || $name['name'] != $data['company_name']) {
            $this->json('1000', '请检查您的营业执照！', null);
        }
		$this->examine_image($data['legal_card'], $data['legal_name']);//验证身份证
		
        //负责人信息
        $data['nick'] = $form3['name'];
        $data['alipay'] = $form3['alipay'];
		$data['card'] = trim(input('post.card'));
        $data['phone'] = $form3['phone'];
        $data['validity_time'] = trim(input('post.date'));
		$mytime = date("Y-m-d", strtotime("1 year"));
        if ($data['validity_time'] < $mytime) {
            $this->json('1000', '证件有效期必须大于入驻时间1年！', null);
        }
        if (!empty($_FILES['charge_face']['name'])) {
            $files = request()->file('charge_face');
            $img7 = $news_list->upload($files, 'Images/authentication');
            if (!$img7) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['charge_face'] = $img7;
        }
        if (!empty($_FILES['charge_back']['name'])) {
            $files = request()->file('charge_back');
            $img8 = $news_list->upload($files, 'Images/authentication');
            if (!$img8) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['charge_back'] = $img8;
        }
        if (!empty($_FILES['authorization_image']['name'])) {
            $files = request()->file('authorization_image');
            $img9 = $news_list->upload($files, 'Images/company');
            if (!$img9) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['authorization_image'] = $img9;
        }
		
		$this->examine_image($data['card'], $data['nick']);//验证身份证
        $data['time'] = time();
        $data['static'] = 1;
        $form_list = new order_list('authentication_enterprise');
        $res = $form_list->add_form($data);
        if (isset($res) && !empty($res)) {
            action_log_index('个人中心企业认证添加', 'log_ment', 1);
            $this->json('200', '数据添加成功', $res);
        } else {
            action_log_index('个人中心企业认证添加', 'log_ment');
            $this->json('1000', '数据添加失败', null);
        }
    }

    //企业认证修改
    public function company_edit()
    {
        $where['id'] = trim(input('post.id'));
        $media1 = trim(input('post.media1JsonString'));
        $media2 = trim(input('post.media2JsonString'));
        $media3 = trim(input('post.media3JsonString'));
        $form1 = json_decode($media1, true);
        $form2 = json_decode($media2, true);
        $form3 = json_decode($media3, true);
        $news_list = new News();
        if (!empty($_FILES['business_img']['name'])) {
            $files = request()->file('business_img');
            $img1 = $news_list->upload($files, 'Images/company');
            if (!$img1) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['business_pic'] = $img1;
        }
        if (!empty($_FILES['code_img']['name'])) {
            $files = request()->file('code_img');
            $img2 = $news_list->upload($files, 'Images/company');
            if (!$img2) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['code_pic'] = $img2;
        }
        if (!empty($_FILES['tax_img']['name'])) {
            $files = request()->file('tax_img');
            $img3 = $news_list->upload($files, 'Images/company');
            if (!$img3) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['tax_pic'] = $img3;
        }
        if ($form1['organization'] == 'normal') {
            $data['c_type'] = 1;
            if ($form1['paper'] == 1) {
                $data['d_type'] = 1;
            } else {
                $data['d_type'] = 2;
                //本地上传
                if (!empty($_FILES['single']['name'])) {
                    $files = request()->file('single');
                    $img4 = $news_list->upload($files, 'Images/company');
                    if (!$img4) {
                        $this->json('1000', '图片上传失败', null);
                    }
                    $data['business_pic'] = $img4;
                }
            }
            $data['company_name'] = $form1['enterprises'];
            $data['regist_number'] = $form1['enterpriseNumber'];
        } else {
            $data['c_type'] = 2;
            $data['d_type'] = 1;
            $data['company_name'] = $form1['institutions'];
            $data['regist_number'] = $form1['cards'];
        }
        $data['company_year'] = $form1['date'];
        $data['o_code'] = $form1['organization_code'];
        $data['tax_number'] = $form1['tax'];
		if (strlen($data['tax_number']) == 15 || strlen($data['tax_number']) == 17 || strlen($data['tax_number']) == 18 || strlen($data['tax_number']) == 20) {
        } else {
            $this->json('1000', '税务登记号格式不正确！', null);
            return false;
        }
        $re1 = "/^([0-9])|((?=.*?[0-9])(?=.*?[a-zA-Z])[a-zA-Z0-9])$/";
        if (!preg_match($re1, $data['tax_number'])) {
            $this->json('1000', '税务登记号格式不正确！', null);
            return false;
        }
		 if ($data['business_pic']) {
            $name = $this->examine_file($data['business_pic']);
            if (!$name) {
                $this->json('1000', '请检查您的营业执照！', null);
            }
            if ($name['person'] != $data['legal_name'] || $name['name'] != $data['company_name']) {
                $this->json('1000', '请检查您的营业执照！', null);
            }
        }
        //法人信息
        $data['legal_card'] = trim(input('post.legal_card'));
        $data['legal_date'] = trim(input('post.legal_date'));
        $data['legal_name'] = $form2['legal_name'];
		$mytime = date("Y-m-d", strtotime("1 year"));
        if ($data['legal_date'] < $mytime) {
            $this->json('1000', '证件有效期必须大于入驻时间1年！', null);
        }
        if (!empty($_FILES['legal_face']['name'])) {
            $files = request()->file('legal_face');
            $img5 = $news_list->upload($files, 'Images/authentication');
            if (!$img5) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['legal_face'] = $img5;
        }
        if (!empty($_FILES['legal_back']['name'])) {
            $files = request()->file('legal_back');
            $img6 = $news_list->upload($files, 'Images/authentication');
            if (!$img6) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['legal_back'] = $img6;
        }
		
		$this->examine_image($data['legal_card'], $data['legal_name']);//验证身份证
		
        //负责人信息
        $data['nick'] = $form3['name'];
        $data['alipay'] = $form3['alipay'];
        $data['card'] = trim(input('post.card'));
        $data['phone'] = $form3['phone'];
        $data['validity_time'] = trim(input('post.date'));
		$mytime = date("Y-m-d", strtotime("1 year"));
        if ($data['validity_time'] < $mytime) {
            $this->json('1000', '证件有效期必须大于入驻时间1年！', null);
        }
        if (!empty($_FILES['charge_face']['name'])) {
            $files = request()->file('charge_face');
            $img7 = $news_list->upload($files, 'Images/authentication');
            if (!$img7) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['charge_face'] = $img7;
        }
        if (!empty($_FILES['charge_back']['name'])) {
            $files = request()->file('charge_back');
            $img8 = $news_list->upload($files, 'Images/authentication');
            if (!$img8) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['charge_back'] = $img8;
        }
        if (!empty($_FILES['authorization_image']['name'])) {
            $files = request()->file('authorization_image');
            $img9 = $news_list->upload($files, 'Images/company');
            if (!$img9) {
                $this->json('1000', '图片上传失败', null);
            }
            $data['authorization_image'] = $img9;
        }
		
		$this->examine_image($data['card'], $data['nick']);//验证身份证
		
        $data['static'] = 1;
        $form_list = new order_list('authentication_enterprise');
        $res = $form_list->edit_form($where, $data);
        if (isset($res) && !empty($res)) {
            action_log_index('个人中心企业认证修改', 'log_ment', 1);
            $this->json('200', '数据修改成功', $res);
        } else {
            action_log_index('个人中心企业认证修改', 'log_ment');
            $this->json('1000', '数据修改失败', null);
        }
    }

    /**
     * 交易类商品首页
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function trading()
    {
        $where['services'] = 1;
        $uid = trim(input('get.uid'));
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;

      
        $status = (isset($_GET['statuc']) && !empty($_GET['statuc'])) ? $_GET['statuc'] : array('neq', 0);
        $where2['statuc'] = array('in', array(3, 4, 5, 6, 7));
		$where5['statuc'] = array('in', array(10,11,12,13,14));


        $where3['uid'] = $uid;
        $where3['static'] = array('in', array(1,3));
        $where4['username'] = $uid;
        $where4['services'] = 1;
        $allcount = db('offer')->where($where3)->select();
        $all = db('orders')->where($where4)->select();
        $res['all_count'] = count($allcount) + count($all);
        foreach ($all as $key => $value) {
            foreach ($allcount as $k => $v) {
                if ($value['offer_id'] == $v['id']) {
                    $res['all_count'] = $res['all_count'] - 1;
                }
            }
        }

        $where['username'] = $uid;
        $res['count1'] = db('orders')->where($where)->where('statuc', 2)->count();
        $res['count2'] = db('orders')->where($where)->where($where2)->count();
        $res['count4'] = db('orders')->where($where)->where('statuc', 9)->count();
        $res['count5'] = db('orders')->where($where)->where($where5)->count();

        if ($status == 4) {
            $where1['a.statuc'] = array('in', array(3, 4, 5, 6, 7));
			$where1['o.static'] = 4;
        } else if($status == 10) {
            $where1['a.statuc'] = array('in', array(10,11,12,13,14));
			$where1['o.static'] = 4;
        }else if($status == 2){
			$where1['a.statuc'] = 2;
			$where1['o.static'] = array('in',array(2,4));
		}else if($status == 9){
			$where1['a.statuc'] = 9;
			$where1['o.static'] = array('in',array(1,2,4));
		}else{
			$where1['a.statuc'] = $status;
		}
        $where1['o.uid'] = $uid;
        $where1['a.services'] = 1;

        $join[0] = "__MARKET_TRADING__ m";
        $join[1] = "m.id = a.name";
        $jo[0] = "__MARKET_TRADING_CATEGORY__ t";
        $jo[1] = "t.id = m.category_id";
        $ji[0] = "__MARKET_TRADING_CID__ c";
        $ji[1] = "c.id = m.c_id";
        $join3[0] = "__SHOP_INFORMATION__ n";
        $join3[1] = "n.id = m.name";
        $join4[0] = "__OFFER__ o";
        $join4[1] = "a.id = o.order_id";

        $filed = "a.id,a.order,a.contract,a.seller_e,a.name as shop_id,a.seller,a.statuc,a.s_time,a.time,a.username as u_id,m.id as name_id,m.image,m.username,m.store,t.market_name,c.trading_name,n.name,o.static,o.store as money";
        $res['order'] = db('orders')->order('id desc')->alias('a')->limit(10)->page($page)->join($join[0], $join[1])
            ->join($jo[0], $jo[1])->join($ji[0], $ji[1])->join($join3[0], $join3[1])->join($join4[0], $join4[1])->field($filed)->where($where1)->select();


//            //分页
        if ($status == 2) {
			$res['count'] = $res['count1'];
            $Page = new \app\api\model\Page($res['count1'], 10);
        } else if ($status == 4) {
			$res['count'] = $res['count2'];
            $Page = new \app\api\model\Page($res['count2'], 10);
        } else if ($status == 9) {
			$res['count'] = $res['count4'];
            $Page = new \app\api\model\Page($res['count4'], 10);
        } else if ($status == 10) {
			$res['count'] = $res['count5'];
            $Page = new \app\api\model\Page($res['count5'], 10);
        } else {
			$res['count'] = $res['all_count'];
            $Page = new \app\api\model\Page($res['all_count'], 10);
        }
        $render = $Page->render;

        $res['render'] = $render;

        foreach ($res['order'] as $key => $value) {
            if ($value['s_time'] <= time()) { // 如果过了活动终止日期
                $res['order'][$key]['shi_time'] = '0天0时0分';
            } else {

                // 使用当前日期时间到活动截至日期时间的毫秒数来计算剩余天时分
                $time = $value['s_time'] - time();

                $days = 0;
                if ($time >= 86400) { // 如果大于1天
                    $days = (int)($time / 86400);
                    $time = $time % 86400; // 计算天后剩余的毫秒数
                }

                $xiaoshi = 0;
                if ($time >= 3600) { // 如果大于1小时
                    $xiaoshi = (int)($time / 3600);
                    $time = $time % 3600; // 计算小时后剩余的毫秒数
                }

                $fen = (int)($time / 60); // 剩下的毫秒数都算作分

                $res['order'][$key]['shi_time'] = $days . '天' . $xiaoshi . '时' . $fen . '分';
            }
        }
        $this->_jsonp('200', 'OK', $res);
    }

    /**
     * 取消订单
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function trading_edit()
    {
        $where['id'] = trim(input('get.id'));
        $res = db('orders')->where($where)->find();
        if ($res['status'] == 2 && $res['statuc'] == 2) {
            $data['status'] = 12;
            $data['statuc'] = 12;
            db('orders')->where($where)->update($data);

			$data1['static'] = 4;
            $where1['id'] = $res['offer_id'];
            db('offer')->where($where1)->update($data1);
            action_log_index('交易中心取消订单', 'log_ment', 1);
            $this->_jsonp('200', '取消成功', null);
        } else {
            action_log_index('交易中心取消订单', 'log_ment');
            $this->_jsonp('1000', '取消失败', null);
        }
    }

    /**
     * 付款
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function price()
    {
        $where['username'] = trim(input('get.uid'));
        $where['id'] = trim(input('get.id'));
        $data1['address_username'] = trim(input('get.address_username'));
        //启动事务

//                Db::name('order_msg')->insert($data);
        $flag = Db::name('orders')->where($where)->update($data1);
        if ($flag) {
            $this->_jsonp('200', '提交成功', null);
        } else {
            $this->_jsonp('1000', '提交失败', null);
        }
    }

    /**
     * 下载
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function trading_download()
    {
        $where['id'] = trim(input('get.id'));//订单id
        $res = db('orders')->where($where)->find();
        if ($res['statuc'] == 4) {
            $data['order_status'] = '5';
            $data['order_num'] = $where['id'];
            $data['time'] = time();
            $data['order_msg'] = '文件下载成功,待确认文件';

            $data1['statuc'] = 5;
            $data1['status'] = 3;
            //启动事务
            Db::startTrans();
            try {
                Db::name('order_msg')->insert($data);
                Db::name('orders')->where($where)->update($data1);
                Db::commit();
                $standard = 2;
            } catch (\Exception $e) {
                Db::rollback();
            }
            if ($standard == 2) {
                $this->_jsonp('200', '提交成功', null);
            } else {
                $this->_jsonp('1000', '提交失败', null);
            }
            action_log_index('个人中心订单文件下载', 'log_ment', 1);
            $this->_jsonp('200', '下载成功', null);
        } else {
            action_log_index('个人中心订单文件下载', 'log_ment');
            $this->_jsonp('1000', '下载失败', null);
        }
    }


    /**
     * 确认合同
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function trading_confirm()
    {
        $where['id'] = trim(input('get.id'));//订单id
        $res = db('orders')->where($where)->find();
        if ($res['statuc'] == 5) {
            $data['order_status'] = '6';
            $data['order_num'] = $where['id'];
            $data['time'] = time();
            $data['order_msg'] = '待卖家上传快递单号';

            $data1['statuc'] = 6;
            $data1['status'] = 5;
            //启动事务
            Db::startTrans();
            try {
                Db::name('order_msg')->insert($data);
                Db::name('orders')->where($where)->update($data1);
                Db::commit();
                $standard = 2;
            } catch (\Exception $e) {
                Db::rollback();
            }
            if ($standard == 2) {
                $this->_jsonp('200', '提交成功', null);
            } else {
                $this->_jsonp('1000', '提交失败', null);
            }
            action_log_index('个人中心确认文件', 'log_ment', 1);
            $this->_jsonp('200', '确认成功', null);
        } else {
            action_log_index('个人中心确认文件', 'log_ment');
            $this->_jsonp('1000', '确认失败', null);
        }
    }

    /**
     *确认收到文件
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function trading_receive()
    {
        $where['id'] = trim(input('get.id'));//订单id
        $res = db('orders')->where($where)->find();
        if ($res['statuc'] == 7) {
            $data['order_status'] = '9';
            $data['order_num'] = $where['id'];
            $data['time'] = time();
            $data['order_msg'] = '文件已确认接收,订单结束';

            $data1['statuc'] = 9;
            $data1['status'] = 7;
            //启动事务
            Db::startTrans();
            try {
                Db::name('order_msg')->insert($data);
                Db::name('orders')->where($where)->update($data1);
                Db::commit();
                $standard = 2;
            } catch (\Exception $e) {
                Db::rollback();
            }
            if ($standard == 2) {
                $this->_jsonp('200', '提交成功', null);
            } else {
                $this->_jsonp('1000', '提交失败', null);
            }
            action_log_index('个人中心确认收到合同', 'log_ment', 1);
            $this->_jsonp('200', '确认成功', null);
        } else {
            action_log_index('个人中心确认收到合同', 'log_ment');
            $this->_jsonp('1000', '确认失败', null);
        }
    }

    /**商家确认收款**/
    public function confirm_money()
    {
        $where1['id'] = trim(input('get.id'));
        $data1['status'] = trim(input('get.status'));
        $data = Db::name('orders')->where($where1)->field('s_id as g_id')->find();
        $where2['id'] = $data['g_id'];
        $where3 = Db::name('service_market')->where($where2)->field('s_id as shop_id')->find();
        $data2['order_num'] = trim(input('get.id'));
        $data2['order_status'] = $data1['status'];
        $data2['order_msg'] = '卖家已收款';
        $data2['time'] = time();
        $standard = 1;
        //启动事务
        Db::startTrans();
        try {
            Db::name('orders')->where($where1)->update($data1);      
            Db::name('order_msg')->insert($data2);
            Db::name('order_comment')->insert($data);
            Db::commit();
            $standard = 2;
        } catch (\Exception $e) {
            Db::rollback();
        }
        if ($standard == 2) {
            action_log_index('商家确认收款', 'log_ment', 1);
            $this->_jsonp('200', '提交成功', null);
        } else {
            action_log_index('商家确认收款', 'log_ment');
            $this->_jsonp('1000', '提交失败', null);
        }
    }

    /**
     * 我的消息主页
     */
    public function message()
    {
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
		$category = (isset($_GET['value']) && !empty($_GET['value'])) ? $_GET['value'] : 1;
		$match = new Preg();
        if ($category == 1) {
			$where['to_uid'] = trim(input('get.uid'));
			$where['reply_type'] = 1;
			$where['index'] = 1;
			$join[0] = "__USER__ u";
			$join[1] = "u.uid = a.from_uid";
			$field = "a.id,a.content,a.time,a.status,a.pid,a.from_uid,u.username";
			$where1['a.status'] = array('in', array(1,2));
			$data2= db('goods_chat')->where($where)->order('status desc')->order('time desc')->alias('a')->join($join[0], $join[1])->where($where1)->limit(10)->page($page)->field($field)->select();
			foreach($data2 as $key=>$val){
                $html=$match->replace($val['content']);
				$data1[$key]['content']=$html;
            }
			$res['data']=$data2;
			$res['count'] = db('goods_chat')->where($where)->count();
		}else{
			$where1['uid'] = trim(input('get.uid'));
            $field = "id,title,content,time,status";
            $where1['status'] = array('in', array(1, 2));
             $data1= db('platform')->order('status desc')->order('time desc')->where($where1)->limit(10)->page($page)->field($field)->select();
            
			foreach($data1 as $key=>$val){
                $html=$match->replace($val['content']);
				$data1[$key]['content']=$html;
            }
			$res['data']=$data1;
			$res['count'] = db('platform')->where($where1)->count();
		}
//        foreach ($res['data'] as $key => $value) {
//            $res['data'][$key] = db('goods_chat')->where('id', $value['pid'])->where($where)->order('time asc')->alias('a')->field($field)->join($join[0], $join[1])->find();
//            if ($res['data'][$key] == null) {
//                $res['data'][$key] = $value;
//            }
//        }
        $Page = new \app\api\model\Page($res['count'], 10);
        $render = $Page->render;

        $res['render'] = $render;

        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }


    /**
     * 更改状态
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del_all()
    {
        $id = trim(input('get.uid'));
		$value = trim(input('get.value'));
        if ($value == 1) {
            $name = 'goods_chat';
        } else {
            $name = 'platform';
        }
        $where['id'] = explode(",", $id);
        $data['status'] = trim(input('get.status'));
        for ($i = 0; $i < count($where['id']); $i++) {
            $res = db($name)->where('id', $where['id'][$i])->update($data);
        }
        if ($res == true) {
            action_log_index('个人中心删除消息', 'log_ment', 1);
            $this->_jsonp('200', '更新成功', $res);
        } else {
            action_log_index('个人中心删除消息', 'log_ment');
            $this->_jsonp('1000', '更新失败', null);
        }
    }


    /**
     * 查询消息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function check_mes()
    {
        $where['id'] = trim(input('get.id'));
        $res = db('goods_chat')->where($where)->find();
        $where1['status'] = array('in', array(1,2));
        $order = "time asc";
        $where1['from_uid'] = $res['to_uid'];
        $where1['to_uid'] = $res['from_uid'];
        $res['data'] = db('goods_chat')->order($order)->where($where1)->select();

        foreach ($res['data'] as $key => $value) {
            $res['data'][$key]['tel'] = db('goods_chat')->order($order)->where('pid', $value['id'])->select();
        }

        if (isset($res) || !empty($res)) {
            $data['status'] = 1;
            db('goods_chat')->where($where)->update($data);
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }
	
	 /**
     * 查询消息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function check_mes2()
    {
        $where['id'] = trim(input('get.id'));
        $res = db('platform')->where($where)->find();
        if (isset($res) || !empty($res)) {
            $data['status'] = 1;
            db('platform')->where($where)->update($data);
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }

    /**
     * 回复
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function check()
    {
        $where['id'] = trim(input('get.id'));
        $king = db('goods_chat')->where($where)->find();
        $data['reply_type'] = 2;
        $data['content'] = trim(input('get.content'));
        $data['from_uid'] = $king['to_uid'];
        $data['to_uid'] = $king['from_uid'];
        $data['pid'] = $king['id'];
        $data['time'] = time();
		$data['index'] = 1;

        $where1['to_uid'] = $king['from_uid'];
        $where1['from_uid'] = $king['to_uid'];
        $where1['reply_type'] = 2;
        $data1['index'] = 0;
        db('goods_chat')->where($where1)->update($data1);
        $res = db('goods_chat')->insert($data);
        if ($res == true) {
            action_log_index('个人中心回复买家消息', 'log_ment', 1);
            $this->_jsonp('200', '更新成功', $res);
        } else {
            action_log_index('个人中心回复买家消息', 'log_ment');
            $this->_jsonp('1000', '更新失败', null);
        }
    }

	
    /**
     * 申诉列表
     */
    public function complain()
    {
        $where['uid'] = trim(input('get.uid'));
        $where['status'] = 1;
        $where['static'] = (isset($_GET['static']) && !empty($_GET['static'])) ? $_GET['static'] : array('neq', 0);
		if($where['static'] == 3){
			$where['static'] = array('in',array(3,4));
		}
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
         $data= db('complain')->where($where)->limit(10)->page($page)->select();
		//屏蔽关键字
		$match = new Preg();
		foreach($data as $key=>$val){
                $html=$match->replace($val['problem']);
				$data[$key]['problem']=$html;
            }
			//
			$res['data']=$data;
        $res['num'] = db('complain')->where($where)->field('static,count(id) number')->group('static')->select();

		$where1['static'] = 1;
		$where1['status'] = 1;
		$where1['uid'] = $where['uid'];
		$where2['static'] = 2;
		$where2['status'] = 1;
		$where2['uid'] = $where['uid'];
		$where3['static'] = array('in',array(3,4));
		$where3['status'] = 1;
		$where3['uid'] = $where['uid'];
		$where4['uid'] = $where['uid'];
		$where4['status'] = 1;
		$where4['static'] = array('neq',0);
        $res['count0'] = db('complain')->where($where4)->count();
        $res['count1'] = db('complain')->where($where1)->count();
        $res['count2'] = db('complain')->where($where2)->count();
        $res['count3'] = db('complain')->where($where3)->count();
		
		if($where['static'] == 1){
			$res['count'] = $res['count1'];
			$Page = new \app\api\model\Page($res['count1'], 10);
		}else if($where['static'] == 2){
			$res['count'] = $res['count2'];
			$Page = new \app\api\model\Page($res['count2'], 10);
		}else if($where['static'] == 3){
			$res['count'] = $res['count3'];
			$Page = new \app\api\model\Page($res['count3'], 10);
		}else{
			$res['count'] = $res['count0'];
			$Page = new \app\api\model\Page($res['count0'], 10);
		}
        
        $render = $Page->render;

        $res['render'] = $render;

        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }

	public function update_code(){
		$where['uid'] = trim(input('get.uid'));
		$res = db('user')->where($where)->value('code');
		if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
		
	}
	public function check_code(){
		$where['uid'] = trim(input('get.uid'));
		$res = db('user')->where($where)->find();
		$code_i =trim(input('get.code'));
		$common = new Common();
        $code= $common->change_pwd($code_i, $res['alipay_rand']);
		if($res['code'] == $code){
			$this->_jsonp('200','支付密码正确，收货成功',null);
		}else{
			$this->_jsonp('1000','支付密码错误',null);
		}
		
	}
}